package com.example.foodwebbackend.controller;

import com.example.foodwebbackend.dto.ApiResponse;
import com.example.foodwebbackend.dto.MerchantFoodDto;
import com.example.foodwebbackend.entity.Merchant;
import com.example.foodwebbackend.entity.MerchantFood;
import com.example.foodwebbackend.service.MerchantService;
import com.example.foodwebbackend.service.MerchantFoodService;
import com.example.foodwebbackend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/merchant")
@CrossOrigin(origins = "*")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;
    
    @Autowired
    private MerchantFoodService merchantFoodService;
    
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取商家信息
     */
    @GetMapping("/{merchantId}")
    public ApiResponse<Merchant> getMerchantInfo(@PathVariable Integer merchantId) {
        try {
            Merchant merchant = merchantService.findById(merchantId);
            if (merchant == null) {
                return ApiResponse.error("商家不存在");
            }
            return ApiResponse.success(merchant);
        } catch (Exception e) {
            return ApiResponse.error("获取商家信息失败: " + e.getMessage());
        }
    }

    /**
     * 更新商家信息
     */
    @PutMapping("/{merchantId}")
    public ApiResponse<Merchant> updateMerchantInfo(
        @PathVariable Integer merchantId,
        @RequestBody Merchant request
    ) {
        try {
            Merchant existingMerchant = merchantService.findById(merchantId);
            if (existingMerchant == null) {
                return ApiResponse.error("商家不存在");
            }
            
            // 更新商家信息
            if (request.getMerchantName() != null) {
                existingMerchant.setMerchantName(request.getMerchantName());
            }
            if (request.getContactPhone() != null) {
                existingMerchant.setContactPhone(request.getContactPhone());
            }
            if (request.getMerchantAddress() != null) {
                existingMerchant.setMerchantAddress(request.getMerchantAddress());
            }
            if (request.getBusinessHours() != null) {
                existingMerchant.setBusinessHours(request.getBusinessHours());
            }
            if (request.getMerchantDescription() != null) {
                existingMerchant.setMerchantDescription(request.getMerchantDescription());
            }
            if (request.getFeatures() != null) {
                existingMerchant.setFeatures(request.getFeatures());
            }
            
            Merchant updatedMerchant = merchantService.updateMerchant(existingMerchant);
            return ApiResponse.success("信息更新成功", updatedMerchant);
        } catch (Exception e) {
            return ApiResponse.error("更新商家信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取商家菜品列表
     */
    @GetMapping("/{merchantId}/foods")
    public ApiResponse<List<MerchantFoodDto>> getMerchantFoods(@PathVariable Integer merchantId) {
        try {
            List<MerchantFoodDto> foods = merchantFoodService.getMerchantFoods(merchantId);
            return ApiResponse.success(foods);
        } catch (Exception e) {
            return ApiResponse.error("获取菜品列表失败: " + e.getMessage());
        }
    }

    /**
     * 添加菜品
     */
    @PostMapping("/{merchantId}/foods")
    public ApiResponse<MerchantFood> addFood(
        @PathVariable Integer merchantId,
        @RequestBody MerchantFood food
    ) {
        try {
            food.setMerchantId(merchantId);
            MerchantFood newFood = merchantFoodService.addFood(food);
            return ApiResponse.success("菜品添加成功", newFood);
        } catch (Exception e) {
            return ApiResponse.error("添加菜品失败: " + e.getMessage());
        }
    }

    /**
     * 更新菜品
     */
    @PutMapping("/{merchantId}/foods/{foodId}")
    public ApiResponse<MerchantFood> updateFood(
        @PathVariable Integer merchantId,
        @PathVariable Integer foodId,
        @RequestBody MerchantFood food
    ) {
        try {
            food.setFoodId(foodId);
            food.setMerchantId(merchantId);
            MerchantFood updatedFood = merchantFoodService.updateFood(food);
            if (updatedFood == null) {
                return ApiResponse.error("菜品不存在");
            }
            return ApiResponse.success("菜品更新成功", updatedFood);
        } catch (Exception e) {
            return ApiResponse.error("更新菜品失败: " + e.getMessage());
        }
    }

    /**
     * 删除菜品
     */
    @DeleteMapping("/{merchantId}/foods/{foodId}")
    public ApiResponse<?> deleteFood(
        @PathVariable Integer merchantId,
        @PathVariable Integer foodId
    ) {
        try {
            boolean deleted = merchantFoodService.deleteFood(foodId, merchantId);
            if (!deleted) {
                return ApiResponse.error("菜品不存在或无权限删除");
            }
            return ApiResponse.success("菜品删除成功");
        } catch (Exception e) {
            return ApiResponse.error("删除菜品失败: " + e.getMessage());
        }
    }

    /**
     * 更新菜品状态
     */
    @PutMapping("/{merchantId}/foods/{foodId}/status")
    public ApiResponse<?> updateFoodStatus(
        @PathVariable Integer merchantId,
        @PathVariable Integer foodId,
        @RequestBody Map<String, Boolean> request
    ) {
        try {
            Boolean isActive = request.get("isActive");
            if (isActive == null) {
                return ApiResponse.error("状态值不能为空");
            }
            
            boolean updated = merchantFoodService.updateFoodStatus(foodId, merchantId, isActive);
            if (!updated) {
                return ApiResponse.error("菜品不存在或无权限修改");
            }
            
            return ApiResponse.success("状态更新成功");
        } catch (Exception e) {
            return ApiResponse.error("更新状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取商家评价数据
     */
    @GetMapping("/{merchantId}/reviews")
    public ApiResponse<List<Map<String, Object>>> getMerchantReviews(@PathVariable Integer merchantId) {
        try {
            // 从数据库获取真实评价数据
            List<Map<String, Object>> reviews = merchantFoodService.getMerchantReviews(merchantId);
            return ApiResponse.success(reviews);
        } catch (Exception e) {
            return ApiResponse.error("获取评价数据失败: " + e.getMessage());
        }
    }
} 